import json

class People:
    def __init__(self,name,age):
        self.name = name
        self.age = age
p1 = People('王二狗',12)


#  将一个对象的属性打包成函数，写成字典

def peopledict(p1):
    # dict1 = {'name':p1.name,'age':p1.age}
    # return dict1
    return p1.__dict__
res = json.dumps(p1,default=peopledict)
print('res------->',res)
#   {"name": "\u738b\u4e8c\u72d7", "age": 12}
print(type(res))
#     json   <class 'str'>


def dict2people(dict1):
    name = dict1['name']
    age = dict1['age']
    p1 = People(name,age)
    return p1

# rng = json.loads(res,object_hook=lambda dict1:People(dict1['name'],dict1['age']))
rng = json.loads(res,object_hook=dict2people)
print('rng.name-->',rng.name)
print('rng.age-->',rng.age)
print(type(rng))




list1 = [1,2,3]
res = json.dumps(list1)
print(res)
print(type(res))

